﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Truncation.Detector.Windows;

namespace Truncation.Detector.Win32.Controls
{
    class Control
    {
        public int LineNumber { get; set; }
        public int Left { get; set; }
        public int Top { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }

        public int LeftPadding { get; set; }
        public int TopPadding { get; set; }

        public int RightPadding { get; set; }

        public int BottomPadding { get; set; }
        public Size RealSize { get; private set; }
        public string Content { get; set; }
        public String Parent { get; set; }
        protected Control()
        {
            Left = Top = Width = Height = LeftPadding = RightPadding = TopPadding = BottomPadding = 0;
        }

        public virtual bool Measure(IntPtr hdc, double scale = 1.0)
        {
            var txt = Content.Replace("\\n", "\n").Replace("&", String.Empty);
            var rect = new GdiWrap.RECT();
            var height = GdiWrap.DrawText(hdc, txt, txt.Length, ref rect, 0x00000400);
            if (height == 0)
                return false;
            var measuredWidth = (int)(rect.Width() * scale);
            var measuredHeight = (int)(rect.Height() * scale);
            if (measuredWidth <= (Width-LeftPadding-RightPadding) && measuredHeight <= (Height-TopPadding-BottomPadding))
                return true;
            return false;
        }

        protected GdiWrap.SIZE MeasureText(IntPtr hdc, string txt, double scale = 1.0)
        {
            if (String.IsNullOrEmpty(txt))
                return new GdiWrap.SIZE();
            var rect = new GdiWrap.RECT();
            var height = GdiWrap.DrawText(hdc, txt, txt.Length, ref rect, 0x00000400);
            if (height == 0)
                return new GdiWrap.SIZE();
            var measuredWidth = (int)(rect.Width() * scale);
            var measuredHeight = (int)(rect.Height() * scale);
            return new GdiWrap.SIZE() { Width = measuredWidth, Height = measuredHeight };
        }
    }
}
